package com.anjbo.controller.common;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.anjbo.bean.BaseDto;
import com.anjbo.common.Constants;
import com.anjbo.common.RespStatus;
import com.anjbo.common.RespStatusEnum;
import com.anjbo.common.UserAgentUtil;
import com.anjbo.controller.BaseController;
import com.anjbo.utils.HttpUtil;
import com.anjbo.utils.LoginCacheUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统计类
 * @author limh
 *
 */
@Controller
@RequestMapping("/mortgage/statistics")
public class StatisticsController extends BaseController {
	private Logger log = Logger.getLogger(getClass());
	public static final String VERSION = new BaseDto().getVersion();
	
	
	public RespStatus statistics(HttpServletRequest request, String pageCode, String channel){
		try {
			String userAgent = request.getHeader("User-Agent");
			log.info("后台统计User-Agent参数："+userAgent);
			String sid = getSid(request);
			String deviceId = getDeviceId(request);
			String url = Constants.STATISTICS_URL+"statistics/mort/addMortVisit";
			Map<String,String> map = new HashMap<String,String>();
			map.put("appName","mortgage");
			map.put("terminalType",UserAgentUtil.getClientOS(userAgent));
			map.put("osType",UserAgentUtil.getBrowserInfo(userAgent));
			map.put("osName",getDevice(request));
			map.put("pageCode",pageCode);
			map.put("userType",String.valueOf(NumberUtils.toInt(LoginCacheUtil.getUserType(sid, deviceId))));
			map.put("uid", LoginCacheUtil.getUid(sid, deviceId));
			map.put("sid",sid);
			map.put("deviceId",deviceId);
			map.put("version",channel);
			try {
				String city = request.getParameter("city");
				if(StringUtils.isNotEmpty(city)){
					city = URLDecoder.decode(city, "utf-8");
				}else{
					city = ObjectUtils.toString(request.getHeader("city"));
					if(StringUtils.isEmpty(city)){
						city = "深圳";
					}
				}
				map.put("city",city);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			try {
				String result = HttpUtil.post(url, map);
				log.info("调用统计返回："+result);
				return new RespStatus(RespStatusEnum.SUCCESS.getCode(),
						RespStatusEnum.SUCCESS.getMsg());
			} catch (Exception e) {
				e.printStackTrace();
				return new RespStatus(RespStatusEnum.FAIL.getCode(),e.getMessage());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new RespStatus(RespStatusEnum.FAIL.getCode(),e.getMessage());
		}
	}
	/**
	 * 统计页面访问
	 * @author limh
	 * @date 2015-8-20 下午04:23:56
	 * @param request
	 * @param pageCode
	 */
	@RequestMapping(value = "/statistics")
	public @ResponseBody
	RespStatus statistics(HttpServletRequest request,String pageCode){
		return statistics(request, pageCode,VERSION);
	}
	/**
	 * 统计登录
	 * @Title: statisticsLogin 
	 * @param request
	 * @param uid
	 * @param userType
	 * @return
	 * RespStatus
	 * @throws
	 */
	@RequestMapping(value = "/statisticsLogin")
	public @ResponseBody
	RespStatus statisticsLogin(HttpServletRequest request,String uid,String userType){
		String userAgent = request.getHeader("User-Agent");
		log.info("后台统计User-Agent参数："+userAgent);
		String sid = getSid(request);
		String deviceId = getDeviceId(request);
		String url = Constants.STATISTICS_URL+"statistics/statis/addAppLogin";
		Map<String,String> map = new HashMap<String,String>();
		map.put("appName","mortgage");
		map.put("terminalType", UserAgentUtil.getClientOS(userAgent));
		map.put("osType",UserAgentUtil.getBrowserInfo(userAgent));
		map.put("osName",getDevice(request));
		map.put("channel",getDevice(request));
		map.put("uid",uid);
		map.put("userType",String.valueOf(NumberUtils.toInt(userType)));
		map.put("sid",sid);
		map.put("deviceId",deviceId);
		map.put("version",VERSION);
		try {
			HttpUtil.post(url, map);
			return new RespStatus(RespStatusEnum.SUCCESS.getCode(),
					RespStatusEnum.SUCCESS.getMsg());
		} catch (Exception e) {
			e.printStackTrace();
			return new RespStatus(RespStatusEnum.FAIL.getCode(),e.getMessage());
		}
	}
	/**
	 * 统计注册
	 * @Title: statisticsReg 
	 * @param request
	 * @param mobile
	 * @param userType
	 * @return
	 * RespStatus
	 * @throws
	 */
	@RequestMapping(value = "/statisticsReg")
	public @ResponseBody
	RespStatus statisticsReg(HttpServletRequest request,String mobile,String userType){
		String userAgent = request.getHeader("User-Agent");
		log.info("后台统计User-Agent参数："+userAgent);
		String sid = getSid(request);
		String deviceId = getDeviceId(request);
		String url = Constants.STATISTICS_URL+"statistics/statis/addAppReg";
		Map<String,String> map = new HashMap<String,String>();
		map.put("appName","mortgage");
		map.put("terminalType",UserAgentUtil.getClientOS(userAgent));
		map.put("osType",UserAgentUtil.getBrowserInfo(userAgent));
		map.put("osName",getDevice(request));
		map.put("channel",getDevice(request));
		map.put("mobile",mobile);
		map.put("userType",String.valueOf(NumberUtils.toInt(userType)));
		map.put("sid",sid);
		map.put("deviceId",deviceId);
		map.put("version",VERSION);
		try {
			HttpUtil.post(url, map);
			return new RespStatus(RespStatusEnum.SUCCESS.getCode(),
					RespStatusEnum.SUCCESS.getMsg());
		} catch (Exception e) {
			e.printStackTrace();
			return new RespStatus(RespStatusEnum.FAIL.getCode(),e.getMessage());
		}
	}
	
	/**
	 * 统计房产详情浏览
	 * @author lichao
	 * @date 2015-8-20 下午04:23:56
	 * @param request
	 * @param pageCode
	 */
	@RequestMapping(value = "/statisticsHouseDetail")
	public @ResponseBody
	RespStatus statisticsHouseDetail(HttpServletRequest request,String houseId){
		try {
			String userAgent = request.getHeader("User-Agent");
			String terminalType = request.getHeader("terminalType");
			String osType = request.getHeader("osType");
			String appSource = request.getHeader("appSource");
			log.info("后台统计User-Agent参数："+userAgent);
			String sid =  request.getHeader("sid");
			String deviceId = request.getHeader("deviceId");
			String device = request.getHeader("device");
			String versionHeader = request.getHeader("version");
			String url = Constants.STATISTICS_URL+"statistics/statis/addHouseDetailVisit";
			Map<String,String> map = new HashMap<String,String>();
			
			map.put("uid", LoginCacheUtil.getUid(sid, deviceId));
			map.put("houseId",houseId);
			map.put("appName","mortgage");
			map.put("terminalType",StringUtils.isNotEmpty(terminalType)?terminalType:UserAgentUtil.getClientOS(userAgent));
			map.put("osType",StringUtils.isNotEmpty(osType)?osType:UserAgentUtil.getBrowserInfo(userAgent));
			map.put("osName",device);
			map.put("version",StringUtils.isNotEmpty(versionHeader)?versionHeader:VERSION);
			map.put("appSource",appSource);
			try {
				String city = request.getHeader("city");
				if(StringUtils.isNotEmpty(city)){
					city = URLDecoder.decode(city, "utf-8");
				}else{
					city = "深圳";
				}
				map.put("city",city);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			
			try {
				String result = HttpUtil.post(url, map, "utf-8");
				log.info("调用统计返回："+result);
				return new RespStatus(RespStatusEnum.SUCCESS.getCode(),
						RespStatusEnum.SUCCESS.getMsg());
			} catch (Exception e) {
				e.printStackTrace();
				return new RespStatus(RespStatusEnum.FAIL.getCode(),e.getMessage());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new RespStatus(RespStatusEnum.FAIL.getCode(),e.getMessage());
		}
	}

}